package com.banshuibao.bsb_android.activity;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.CardRechargeModel;
import com.banshuibao.bsb_android.util.IntentManager;
import com.banshuibao.bsb_android.util.XToast;

public class ToVipActivity extends BaseActivity implements View.OnClickListener {

    private TextView buy, exchange;

    private PopupWindow popupWindow;
    private EditText card_number;
    private EditText card_pass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_to_vip);
        initView();
    }

    public void initView() {
        setTitleText("购买会员卡");
        exchange = findViewById(R.id.menu);
        exchange.setVisibility(View.VISIBLE);
        exchange.setText("实体卡兑换");
        buy = findViewById(R.id.buy);
        buy.getPaint().setFakeBoldText(true);
       TextView vip = findViewById(R.id.icon_vip);
       vip.getPaint().setFakeBoldText(true);
        exchange.setOnClickListener(this);
        buy.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.menu:
                showDialog(view);
                break;
            case R.id.buy:
                IntentManager.goPayResultActivity(ToVipActivity.this, "", "365","", 6);
                break;
            case R.id.close:
                if(popupWindow!=null&&popupWindow.isShowing()){
                    popupWindow.dismiss();
                }
                break;
            case R.id.change:
                getContent();
                break;
        }
    }

    public void getContent(){
        String number = card_number.getText().toString().trim();
        String pass = card_pass.getText().toString().trim();
        if(TextUtils.isEmpty(number)){
            XToast.showToast(this,"请输入卡号");
            return;
        }
        if(TextUtils.isEmpty(pass)){
            XToast.showToast(this,"请输入密码");
            return;
        }
       requestNet(number,pass);
    }

    public void requestNet(String number,String pass){
        final CardRechargeModel cardRechargeModel = new CardRechargeModel();
        cardRechargeModel.setCardNum(Long.valueOf(number));
        cardRechargeModel.setSecurityCode(pass);
        showProgressDialog();
        HttpUtil.doPost(Constants.Url.CARD_RECHARGE, cardRechargeModel, new HttpResponse(this) {
            @Override
            public void onResult(Object obj) {
                XToast.showToast(ToVipActivity.this,"兑换成功，请重新登录，确认信息准确！");
                IntentManager.goLoginActivity(ToVipActivity.this);
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                cancelProgressDialog();

            }
        });
    }
    public void showDialog(View v) {
        View view = View.inflate(context, R.layout.buy_vip_popuwindows_layout, null);
        popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
        popupWindow.setBackgroundDrawable(context.getResources().getDrawable(android.R.color.transparent));
        popupWindow.setOutsideTouchable(false);
        //设置SelectPicPopupWindow的View
        popupWindow.setContentView(view);
        //设置SelectPicPopupWindow弹出窗体可点击
        popupWindow.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        popupWindow.setAnimationStyle(R.style.PopupWindowns_animation);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        popupWindow.setBackgroundDrawable(dw);
        ImageView close = view.findViewById(R.id.close);
        card_number = view.findViewById(R.id.card_number);
        card_pass = view.findViewById(R.id.card_pass);
        TextView change = view.findViewById(R.id.change);
        close.setOnClickListener(this);
        change.setOnClickListener(this);
        popupWindow.showAsDropDown(v);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK){
            IntentManager.goLoginActivity(ToVipActivity.this);
            XToast.showToast(this,"购买成功请重新登录，确保信息准确！");
        }
    }
}
